package org.dromara.shopping.controller;

import java.util.Arrays;
import java.util.Date;
import java.util.List;

import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import lombok.RequiredArgsConstructor;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.constraints.*;
import cn.dev33.satoken.annotation.SaCheckPermission;
import org.dromara.common.mybatis.core.page.PageUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import org.dromara.common.idempotent.annotation.RepeatSubmit;
import org.dromara.common.log.annotation.Log;
import org.dromara.common.web.core.BaseController;
import org.dromara.common.mybatis.core.page.PageQuery;
import org.dromara.common.core.domain.R;
import org.dromara.common.core.validate.AddGroup;
import org.dromara.common.core.validate.EditGroup;
import org.dromara.common.log.enums.BusinessType;
import org.dromara.common.excel.utils.ExcelUtil;
import org.dromara.shopping.domain.ProductRule;
import org.dromara.shopping.service.IProductRuleService;
import org.dromara.common.mybatis.core.page.TableDataInfo;

/**
 * 商品规则值(规格)
 *
 * @author Mr.Zhou
 * @date 2024-10-21
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/shopping/product/rule")
public class ProductRuleController extends BaseController {

    private final IProductRuleService productRuleService;

    /**
     * 查询商品规则值(规格)列表
     */
    @SaCheckPermission("shopping:rule:list")
    @GetMapping("/list")
    public TableDataInfo<ProductRule> list(ProductRule productRule, PageQuery pageQuery) {
        QueryWrapper<ProductRule> qw = new QueryWrapper<>();
        if(StrUtil.isNotEmpty(productRule.getRuleName())){
            qw.like("rule_name", productRule.getRuleName());
        }

        TableDataInfo<ProductRule> td = PageUtils.buildDataInfo(productRuleService.page(PageUtils.buildPage(),qw));
        List<ProductRule> list = td.getRows();
        list.forEach(rule->{
            if(StrUtil.isNotEmpty(rule.getRuleValue())){
                rule.setRuleValueArray(JSONUtil.parseArray(rule.getRuleValue()));
            }
        });
        return td;
    }



    /**
     * 查询商品规则值(规格)列表
     */
    @Log(title ="查询商品规则值(规格)列表")
    @GetMapping("/listNoPage/{ruleId}")
    public R listNoPage(@PathVariable("ruleId") Integer ruleId) {
        List<ProductRule> list = productRuleService.getRuleListByCateChildId(ruleId);
        list.forEach(rule->{
            if(StrUtil.isNotEmpty(rule.getRuleValue())){
                rule.setRuleValueArray(JSONUtil.parseArray(rule.getRuleValue()));
            }
        });
        return R.ok(list);
    }

    /**
     * 导出商品规则值(规格)列表
     */
    @SaCheckPermission("shopping:rule:export")
    @Log(title = "商品规则值(规格)", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(ProductRule productRule, HttpServletResponse response) {
        List<ProductRule> list = productRuleService.list();
        ExcelUtil.exportExcel(list, "商品规则值(规格)", ProductRule.class, response);
    }

    /**
     * 获取商品规则值(规格)详细信息
     *
     * @param id 主键
     */
    @SaCheckPermission("shopping:rule:query")
    @GetMapping("/{id}")
    public R<ProductRule> getInfo(@NotNull(message = "主键不能为空") @PathVariable Long id) {
        ProductRule productRule = productRuleService.getById(id);
        if(productRule != null){
            if(StrUtil.isNotEmpty(productRule.getRuleValue())){
                productRule.setRuleValueArray(JSONUtil.parseArray(productRule.getRuleValue()));
            }
        }
        return R.ok(productRule);
    }

    /**
     * 新增商品规则值(规格)
     */
    @SaCheckPermission("shopping:rule:add")
    @Log(title = "商品规则值(规格)", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody ProductRule productRule) {
        return toAjax(productRuleService.save(productRule));
    }

    /**
     * 修改商品规则值(规格)
     */
    @SaCheckPermission("shopping:rule:edit")
    @Log(title = "商品规则值(规格)", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody ProductRule productRule) {
        return toAjax(productRuleService.updateById(productRule));
    }

    /**
     * 删除商品规则值(规格)
     *
     * @param ids 主键串
     */
    @SaCheckPermission("shopping:rule:remove")
    @Log(title = "商品规则值(规格)", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")  @PathVariable Long[] ids) {
        return toAjax(productRuleService.removeByIds(Arrays.asList(ids)) ? 1 : 0);
    }
}
